﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RightControl.Common {
    public class Compress {
        public static byte[] GZip(string str) {
            return GZip(Encoding.UTF8.GetBytes(str));
        }

        public static byte[] GZip(byte[] data) {
            using (MemoryStream memoryStream = new MemoryStream()) {
                GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress);
                gZipStream.Write(data, 0, data.Length);
                gZipStream.Close();
                return memoryStream.ToArray();
            }
        }

        public static byte[] GUnZip(byte[] data) {
            using (MemoryStream ms = new MemoryStream(data)) {
                return GUnZip(ms);
            }
        }

        public static byte[] GUnZip(MemoryStream ms) {
            GZipStream gZipStream = new GZipStream(ms, CompressionMode.Decompress);
            using (MemoryStream memoryStream = new MemoryStream()) {
                byte[] array = new byte[4096];
                while (true) {
                    int num = gZipStream.Read(array, 0, array.Length);
                    if (num <= 0) {
                        break;
                    }
                    memoryStream.Write(array, 0, num);
                }
                gZipStream.Close();
                return memoryStream.ToArray();
            }
        }


    }
}